package com.klei.controller;

import com.klei.pojo.Car;
import com.klei.service.ICarService;
import com.klei.util.LayuiJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class CarController {

    @Autowired
    private ICarService carService;

    @GetMapping("/getAllCar")
    @ResponseBody
    public LayuiJson getAllCar(){

        LayuiJson layuiJson = new LayuiJson();
        layuiJson.setCode(0);
        layuiJson.setMsg("");
        layuiJson.setData(carService.getAllCar());

        return layuiJson;
    }

    @GetMapping("/getAllCarByPage")
    @ResponseBody
    public LayuiJson getAllCarByPage(@RequestParam(name = "page") int page,
                                     @RequestParam(name = "limit") int limit){

        LayuiJson layuiJson = new LayuiJson();
        layuiJson.setCode(0);
        layuiJson.setMsg("");
        layuiJson.setData(carService.getCarByPage((page - 1) * limit, limit));
        layuiJson.setCount(carService.getCarCount());

        return layuiJson;
    }

    @GetMapping("/getCarByCid/{bmsel}/{hcs}")
    @ResponseBody
    public List<Car> getCarByCid(@PathVariable("bmsel") int cid,
                                 @PathVariable("hcs") int hccid){

        return carService.getCarByCid(cid);
    }

    @GetMapping("/getCarByRedu/{bmsel}/{hcs}")
    @ResponseBody
    public List<Car> getCarByRedu(@PathVariable("bmsel") int cid,
                                 @PathVariable("hcs") int hccid){

        return carService.getCarByRedu(cid);
    }

    @GetMapping("/getCarById/{id}")
    @ResponseBody
    public Car getCarById(@PathVariable("id") int id){

        return carService.getCarById(id);
    }

    @PostMapping("/updateCar")
    @ResponseBody
    public boolean updateCar(@RequestBody Car car){
        return carService.updateCar(car);
    }

    @PostMapping("/insertCar")
    @ResponseBody
    public boolean insertCar(@RequestBody Car car){
        return carService.insertCar(car);
    }

    @DeleteMapping("/deleteCar/{id}")
    @ResponseBody
    public boolean deleteCar(@PathVariable("id") int id){
        return carService.deleteCar(id);
    }

}
